package com.pv.bfc.model;

import java.sql.Blob;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.transaction.annotation.Transactional;

@Entity
@Transactional
@NamedQueries( {
		@NamedQuery(name = "deleteDocument", query = "delete from Document d where d.id = ?1"),
		@NamedQuery(name = "userDocuments", query = "select d from Document d where d.folderId = ? ") })
@Table(name = "DOCUMENT")
public class Document {

	@Id
	private Integer docId;

	@Column(name = "DESCRIPTION")
	private String description;

	@Column(name = "DOC_SIZE")
	private long size;

	@Column(name = "FOLDERID")
	private Integer folderId;

	@Column(name = "FILENAME")
	private String fileName;

	@Column(name = "FILEPATH")
	private String path;

	@Column(name = "CONTENT")
	@Lob
	private Blob content;

	@Column(name = "CREATED_DATE")
	@Temporal(TemporalType.DATE)
	private Date createdDate;

	public Document() {
	}

	public Document(String fileName, String location) {
		this.fileName = fileName;
		this.path = location;
	}

	public Integer getDocId() {
		return docId;
	}

	public void setDocId(Integer docId) {
		this.docId = docId;
	}

	public Integer getFolderId() {
		return folderId;
	}

	public void setFolderId(Integer folderId) {
		this.folderId = folderId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Blob getContent() {
		return content;
	}

	public void setContent(Blob content) {
		this.content = content;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result
				+ ((description == null) ? 0 : description.hashCode());
		result = PRIME * result + ((docId == null) ? 0 : docId.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Document other = (Document) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (docId == null) {
			if (other.docId != null)
				return false;
		} else if (!docId.equals(other.docId))
			return false;
		return true;
	}

	public String getLocation() {
		String location = this.path + this.fileName;
		return location.replace('\\', '/');
	}

	public String getFileLocation() {
		return System.getProperty("bfc.root") + getLocation();
	}
}
